<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled" class="tabkey-box">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <div class="top-yb">
              <h3>基础信息</h3>
              <a-form-item label="医保目录搜索">
                <JSelectInput name="诊疗项目"  listUrl="wh/medicalServiceProject/miQueryPage" style="width:400px" :columns="gyscolumns" :disabled="yibaoswitch == 'N' || YXSdisableSubmit" placeholder="名称/助记号/编码、模糊匹配"  @change="selectYB" :queryParamCode="['keyword','code','name','pinyinCode']" :queryParamText="['关键字','编码','名称','助记码']"></JSelectInput>
                <!-- <JSelectYaoDian style="width:400px" placeholder="名称/助记号/编码、模糊匹配"  @select="selectYB" :multiple="false" ></JSelectYaoDian> -->
                <!-- <a-input-search style="width:400px" @search="JZSearch" v-model="keywords" placeholder="名称/助记号/编码、模糊匹配"></a-input-search> -->
              </a-form-item>
              <a-form-item label="医保对照">
                <JSwitch v-model="yibaoswitch" :disabled="YXSdisableSubmit" @change="ybdzChange"></JSwitch>
              </a-form-item>
              <dl class="xianjiainfo">
                <dd>
                  <em>限价:</em>
                  <span>{{model.lmtPrice || '--'}}</span>
                </dd>
                <dd>
                  <em>限制使用区划:</em>
                  <span v-if="model.lmtUsedAreas">{{model.lmtUsedAreas}}</span>
                  <span v-else>--</span>
                </dd>
              </dl>
              <!-- <p class="red">启用医保对照时，基础信息的批准文号为只读模式，关闭医保对照时，基础信息的批准文号关闭只读模式。</p> -->
            </div>
          </a-col>
          <a-col :span="8">
            <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="助记码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mnemonic',{}]"  placeholder="请输入助记码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="getConfigStatus('generateDrugCode') || YXSdisableSubmit ?true:false" v-decorator="['code',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="零售价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="YXSdisableSubmit" :precision="2" v-decorator="['retailPrice',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入零售价格" style="width: 100%"/>
            </a-form-item>
          </a-col>
         
          <a-col :span="8">
            <a-form-item label="收费等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['miLevel',{}]" disabled placeholder="请输入收费等级"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始日期" @change="setTimeNext(beginSteps)" v-decorator="['startDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束日期" @change="setTimeNext(beginSteps+1)" v-decorator="['endDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="单品提成" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="YXSdisableSubmit" style="width: 100%;" v-decorator="['singleCommission']" placeholder="请输入单品提成"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="发票类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :async="true" :triggerChange="true" placeholder="请选择"  v-decorator="['projectType',{rules:[ { required: true, message: '请选择!'}]}]" dict="servitem_type">
              </j-search-select-tag>
              <!-- <j-dict-select-tag :triggerChange="true" v-decorator="['projectType',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请选择发票类别" dictCode="servitem_type" /> -->
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input style="width: 100%;" v-decorator="['unit',{rules:[ { required: true, message: '请输入!'}]}]" placeholder="请输入单位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :isHideOne="true" :triggerChange="true" v-decorator="['enabledFlag',{'initialValue':'1'}]" dictCode="depart_status" />
            </a-form-item>
          </a-col>
          <div  style="padding-left: 15px;">
            <a-col :span="24">
              <a-form-item label="项目说明">
                <a-input type="textarea" v-decorator="['projectDesc']" placeholder="请输入项目说明"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="项目内涵">
                <a-input type="textarea" v-decorator="['projectConnotation']" placeholder="请输入"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="除外内容">
                <a-input type="textarea" v-decorator="['exclusionContent']" placeholder="请输入"></a-input>
              </a-form-item>
            </a-col>
          </div>
        </a-row>
        <div class="yb-form" v-if="yibaobotshow && false">
            <a-row :span="24">
              <a-col :span="4">
                <a-form-item label="医保等级" :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled v-decorator="['level']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="4">
                <a-form-item label="医保限价" :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled  v-decorator="['priceLimit']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="职工-自付比例"  :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled v-decorator="['employeeSelfPaymentRatio']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="居民-自付比例"  :labelCol="{span:10}" :wrapperCol="{span:14}">
                  <a-input disabled v-decorator="['residentSelfPaymentRatio']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
              <a-col :span="24">
                <a-form-item label="限制说明：限制用药" >
                  <a-input disabled type="textarea"  v-decorator="['restriction']" placeholder=""></a-input>
                </a-form-item>
              </a-col>
            </a-row>
          </div>
          <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col> -->
       
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JSwitch from '@/components/jeecg/JSwitch'
  import JDate from '@/components/jeecg/JDate'  
  import JSelectInput from '@comp/jeecgbiz/JSelectInput'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import { yaodianFormMinxin } from '@/mixins/yaodianFormMinxin'
  import { keyDownMinxin } from '@/mixins/keyDownMinxin'
  export default {
    name: 'WhDrugWesternForm',
    mixins:[yaodianFormMinxin,keyDownMinxin],
    components: {
      JFormContainer,
      JDate,
      JSwitch,
      JSelectInput,
      JSearchSelectTag
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      //有销售禁用
      YXSdisableSubmit:{
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        yibaobotshow:true,
        keywords:'',
        ybdz:false,
        yibaoswitch:'N',
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          lg: { span: 8 },
          xxl: { span: 6 },
        },
        wrapperCol: {
          lg: { span: 16 },
          xxl: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          splitFlag: {
            rules: [
              { required: true, message: '请输入拆分标志 0:不可拆分 1:可以拆分!'},
            ]
          },
          prescriptionFlag: {
            rules: [
              { required: true, message: '请输入处方药标识 0:非处方 1:处方!'},
            ]
          },
          enabledFlag: {
            rules: [
              { required: true, message: '请输入启用标识 0:禁用 1:启用!'},
            ]
          },
          delFlag: {
            rules: [
              { required: true, message: '请输入删除标识 0:未删除 1:已删除!'},
            ]
          },
        },
        url: {
          add: "/wh/medicalServiceProject/add",
          edit: "/wh/medicalServiceProject/edit",
          saleEdit: "/wh/medicalServiceProject/saleEdit",
          queryById: "/wh/medicalServiceProject/detail",
          repeatCheck:'wh/medicalServiceProject/repeatCheck'
        },
        repeatForm:{
          project:'4'
        },
        gyscolumns: [
          { title: '医保编码', dataIndex: 'medicalCatalogCode', align: 'center', width: '55%' },
          { title: '医保名称', dataIndex: 'medicalCatalogName', align: 'center', width: '45%' },
          // { title: '名称', dataIndex: 'name', align: 'center', width: '45%' },
          // { title: '编码', dataIndex: 'code', align: 'center', width: '45%' },
          // { title: '结束日期', dataIndex: 'endDate', align: 'center', width: '45%' },
          { title: '项目内涵', dataIndex: 'projectConnotation', align: 'center', width: '45%' },
          { title: '项目说明', dataIndex: 'projectDesc', align: 'center', width: '45%' },
          { title: '除外内容', dataIndex: 'exclusionContent', align: 'center', width: '45%' },
          { title: '收费等级', dataIndex: 'miLevel', align: 'center', width: '45%' },
           { title: '限价', dataIndex: 'lmtPrice', align: 'center', width: '45%' },
          
           { title: '限制使用区划', dataIndex: 'lmtUsedAreas', align: 'center', width: '45%'},
          // { title: '项目类别', dataIndex: 'projectType_dictText', align: 'center', width: '45%' },
          // { title: '零售价格', dataIndex: 'retailPrice', align: 'center', width: '45%' },
          // { title: '单品提成', dataIndex: 'singleCommission', align: 'center', width: '45%' },
          // { title: '开始日期', dataIndex: 'startDate', align: 'center', width: '45%' },
          // { title: '单位', dataIndex: 'unit', align: 'center', width: '45%' },
          // { title: '启用状态', dataIndex: 'enabledFlag_dictText', align: 'center', width: '45%' },
          // { title: '零售价格', dataIndex: 'retailPrice', align: 'center', width: '45%' },
          // { title: '单品提成', dataIndex: 'singleCommission', align: 'center', width: '45%' },
        ],
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      },
      beginSteps(){
          var a = 2
          if(this.yibaoswitch == 'Y'){
            a = 3
          }
          return a
      }
    },
    mounted () {

      console.log('456');
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      getCode(){
        if(this.getConfigStatus('generateDrugCode')){
          getAction('wh/medicalServiceProject/getCode',{}).then((res)=>{
            if(res.success){
              this.form.setFieldsValue({'code':res.result.code})
            }
          });
        }
      },
      selectYB(val){
        console.log(val);
        this.model.lmtPrice = val[0].lmtPrice
        this.model.lmtUsedFlag = val[0].lmtUsedFlag
        this.model.lmtUsedAreas = val[0].lmtUsedAreas
        if(this.model.id){
          this.model.medicalCatalogCode = val[0].medicalCatalogCode
          this.model.medicalCatalogName =val[0].medicalCatalogName
          this.form.setFieldsValue(pick(val[0],'miLevel','mnemonic'))
        }else{
          this.model.medicalCatalogCode = val[0].medicalCatalogCode
          this.model.medicalCatalogName =val[0].medicalCatalogName
          this.model.code = val[0].medicalCatalogCode
          this.form.setFieldsValue(pick(val[0],'lmtUsedAreas','name','miLevel','mnemonic','retailPrice','approvalNumber','dose','doseUnit','startDate','endDate','manufacturer','unit','specification','dosageForm','usage','frequency','administration','splitFlag','prescriptionFlag','medicalCatalogCode','delFlag','createBy','createTime','updateBy','updateTime','tenantId','singleCommission','projectType','projectDesc','projectConnotation','exclusionContent'))

        }
        this.setTimeNext(0)
      },
     
      ybdzChange(val){
        console.log(val);
        if(val == 'Y'){
          this.ybdz = true;
          this.form.setFieldsValue({'approvalNumber':''})
        }else{
          this.ybdz = false;
        }
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','mnemonic','code','retailPrice','approvalNumber','dose','doseUnit','startDate','endDate','manufacturer','unit','specification','dosageForm','usage','frequency','administration','splitFlag','prescriptionFlag','enabledFlag','medicalCatalogCode','delFlag','createBy','createTime','updateBy','updateTime','tenantId','singleCommission','projectType','projectDesc','projectConnotation','exclusionContent','miLevel'))
          if(!record.id){
            this.getCode()
            // 获取当前日期
            const today = new Date();
            // 增加年份，5为5年
            today.setFullYear(today.getFullYear() + 5);

            this.form.setFieldsValue({'endDate':today.toISOString().substring(0, 10)})
          }
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true && this.formData.dataId){
         
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
              this.resetKeydown()
            }
          });
        }else{
          this.resetKeydown()
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
             // 新增和编辑时，校验零售价，若高于限价信息，给出提示：零售价高于医保目录限价，是否继续
            that.viodInfo(formData)
           
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','mnemonic','code','retailPrice','approvalNumber','dose','doseUnit','startDate','endDate','manufacturer','unit','specification','dosageForm','usage','frequency','administration','splitFlag','prescriptionFlag','enabledFlag','medicalCatalogCode','delFlag','createBy','createTime','updateBy','updateTime','tenantId','miLevel'))
      },
    }
  }
</script>
<style lang="less" scoped>
.top-yb{
  display: flex;
  align-items: center;
  padding-bottom: 8px;
  h3{
    margin-bottom: 0;
    margin-right: 32px;
  }
  /deep/ .ant-form-item{
    margin-bottom: 0;
    display: flex;
    margin-right: 18px;
  }
  border-bottom: 1px solid #D9D9D9;
  margin-bottom: 20px;
  &>p{
    flex:1;
    color: #FF0000;
  }
}
.yb-form{
  background: #F0F0F0;
  padding: 12px;
}
</style>